package cn.com.acca.ma.dao.impl;

import cn.com.acca.ma.model.BoardIndex;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;

import cn.com.acca.ma.dao.StockInfoDao;
import cn.com.acca.ma.hibernate.util.HibernateUtil;
import cn.com.acca.ma.model.StockInfo;

public class StockInfoDaoImpl extends BaseDaoImpl<StockInfoDaoImpl> implements StockInfoDao {

	public StockInfoDaoImpl() {
		super();
	}

	/**
	 * 通过股票代码stockCode，查找StockInfo对象
	 * @param stockCode
	 * @return
	 */
	public StockInfo getStockInfoObjectByStockCode(String stockCode) {
		logger.debug("通过股票代码stockCode，查找StockInfo对象");
		
		session= HibernateUtil.currentSession();
		session.beginTransaction();
		Criteria criteria=session.createCriteria(StockInfo.class);
		criteria.add(Restrictions.eq("code", stockCode.toUpperCase()));
		StockInfo result=(StockInfo) criteria.uniqueResult();
		session.getTransaction().commit();
		session.close();
		
		return result;
	}

	/**
	 * 获得所有StockInfo对象
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<StockInfo> getAllStockInfo(){
		session= HibernateUtil.currentSession();
		session.beginTransaction();
		Criteria criteria=session.createCriteria(StockInfo.class);
		List<StockInfo> result=criteria.list();
		session.getTransaction().commit();
		session.close();
		return result;
	}
	
	/**
	 * 保存StockInfo对象
	 * @param stockInfo
	 */
	public void saveStockInfo(StockInfo stockInfo){
		logger.info("保存StockInfo对象");
		
		session= HibernateUtil.currentSession();
		session.beginTransaction();
		session.save(stockInfo);
		session.getTransaction().commit();
		session.close();
	}

	/**
	 * 将所有的concept_board_id字段设置为null
	 */
	@Override
	public void setConceptBoardIdNull() {
		logger.info("将所有的concept_board_id字段设置为null");

		session= HibernateUtil.currentSession();
		session.beginTransaction();
		session.createSQLQuery("update stock_info set concept_board_id=null").executeUpdate();
		session.getTransaction().commit();
		session.close();
	}

	/**
	 * 根据code列表，更新concept_board_id列
	 * @param codeList
	 * @param conceptBoardId
	 */
	@Override
	public void updateConceptBoardIdByCodeList(List<String> codeList, Integer conceptBoardId) {
		session= HibernateUtil.currentSession();
		session.beginTransaction();
		String sql = "update stock_info set concept_board_id = :conceptBoardId where code_ in (:codeList)";
		Query query = session.createSQLQuery(sql).setInteger("conceptBoardId", conceptBoardId);
		if (codeList != null && codeList.size() > 0) {
			query.setParameterList("codeList", codeList);
		}
		query.executeUpdate();
		session.getTransaction().commit();
		session.close();
	}
}
